#ifdef MBM_IMIWORLD_SUPPORT

#include "mbm_mstar_inc.h"

#include "mbm_typedefine.h"
#include "mbm_png_adapter.h"
#include "Mbm_font_adapter.h"
#include "Mbm_memory_adapter.h"

#include "ven_graphics.h"

UInt8 *png_data_ptr = 0;

extern u32   miipts_ven_handle;


/**
 * @name mbm_png_decode_init -discripe
 *
 * @param param1	-property	-discripe
 *
 * @return -discripe
 */
void mbm_png_decode_init()
{
	mbm_assert(png_data_ptr==0, "mbm_png_decode_init png_data_ptr!=0");
}

/**
 * @name mbm_png_decode_deinit -discripe
 *
 * @param param1	-property	-discripe
 *
 * @return -discripe
 */
void mbm_png_decode_deinit()
{
	mbm_trace("mbm_png_decode_deinit: png_data_ptr = %x", png_data_ptr);
	
	if(png_data_ptr) 
	{
		impmem_free(png_data_ptr);
		png_data_ptr = 0;
	}
}

typedef void BitmapBase_t;

typedef struct
{
    BitmapBase_t     *pBitmapBase;
    ven_GraphicDIB_t VenDIB;
} _ven_GraphicsBitmap;


extern ven_GraphicsCanvas *g_MiiMainCanvas;

/**
 * @name mbm_png_decode -discripe
 *
 * @param param1	-property	-discripe
 *
 * @return -discripe
 */
void mbm_png_decode( ImpCStr pngName, Int8 **img_p, Int32 *img_w, Int32 *img_h)
{
	UInt16 wPngName[200] = {0};

	UInt8  *data_ptr = 0;
	UInt32 data_size = 0;

	Int32  s_pngfile_handle = 0;
	UInt32 file_size = 0;
	UInt32 read_size = 0;
	UInt8 *pReadFileBuff = NULL;
	UInt16 w = 0;
	UInt16 h = 0;
	UInt16 wPngLen;

	ven_req_data_t openImageCodecReq = {0};
    ven_req_data_t closeImageCodecReq = {0};
    ven_req_data_t decodeImgReq = {0};
	ven_codec_ret_t venCodecRet;
	ven_graphics_ret_t venGraRet;
	Int32 hCodec;
	ven_codec_imgInfo_t imgInfo;        // Image info
	ven_GraphicsBitmap pBmp;           // Bitmap handle
	ven_GraphicBitmapInfo_t bmpInfo;
	ven_codec_decode_param_t decParam = {VEN_ROTATION_ANGLE_0,   {0, 0, 0, 0}};  // Decode parameter
	ven_codec_imageDecodeStatus_t *pDecodeStatus = ven_os_mem_malloc(sizeof(ven_codec_imageDecodeStatus_t));

	ven_GraphicDIB_t pVenDib;
	UInt32 imgSize = 0;
	_ven_GraphicsBitmap *pVenBitmap;
	
	mbm_trace("mbm_png_decode: %s, len = %d", pngName, (Int32)ven_std_utf8strlen(pngName));

	*img_p = 0;
	
//	ven_std_utf8toucs2(pngName, ven_std_utf8strlen(pngName), wPngName, &wPngLen);	
//	s_pngfile_handle = (Int32)ven_file_wfopen(wPngName, "r"); 
	s_pngfile_handle = (Int32)ven_file_wfopen(L"/CARD/1.png", "r"); 

	
	if (!s_pngfile_handle)
	{
		mbm_trace("mbm_png_decode: %s open error.", pngName);
		return;
	}

	if (mbm_fs_getfilesize(s_pngfile_handle, &file_size))
	{
		mbm_trace("mbm_png_decode: %s get file size error.", pngName);
		return;
	}
	else
	{
		mbm_trace("mbm_png_decode: file_size = %d.", file_size);
	}
	
	pReadFileBuff = ven_os_mem_malloc(file_size);
	if (pReadFileBuff == NULL)
	{
		mbm_trace("mbm_png_decode: ven_os_mem_malloc error.");
	}
	
	read_size = ven_file_wfread(pReadFileBuff, file_size, (ven_file_handle_t)s_pngfile_handle);
	if (read_size < 0)
	{
		mbm_trace("mbm_png_decode: ven_file_wfread error. read_size = %d", read_size);
	}
	else
	{
		mbm_trace("mbm_png_decode: read_size = %d", read_size);
	}
	
	// Step 2. Open image codec handle
	openImageCodecReq.bSync = TRUE;
	openImageCodecReq.pfnCb = NULL;
    openImageCodecReq.Handle = miipts_ven_handle;

	venCodecRet = ven_codec_openImgCodec(&hCodec, &openImageCodecReq);
	
    if (VEN_CODEC_SUCCESS != venCodecRet)
    {
        mbm_trace("mbm_png_decode:open image codec error.");
		return;
    }

	// Step 3. Get image information
	venCodecRet = ven_codec_getImageInfo(hCodec, pReadFileBuff, &imgInfo);
    if (VEN_CODEC_SUCCESS != venCodecRet)
    {
        mbm_trace("mbm_png_decode:get image info error.");
		return;
    }

	if (imgInfo.format != VEN_IMAGE_FORMAT_PNG)
	{
		mbm_trace("mbm_png_decode:file type is %d, not png.", imgInfo.format);
		return;
	}

	w = imgInfo.width_p;
	h = imgInfo.height_p;

	*img_w = w;
	*img_h = h;

	mbm_trace("mbm_png_decode:*imgInfo width = %d,height = %d.", *img_w, *img_h);
	
	
	// Step 4. Create bitmap with the given image formation
	bmpInfo.width = w;
    bmpInfo.height = h;
    bmpInfo.nColorScheme = VEN_CLR_SCHEME_RGB565;
    bmpInfo.nColorDepth = 16;
	venGraRet = ven_graphics_createBitmap(&pBmp, &bmpInfo);
    if (VEN_GRAPHICS_RET_SUCCESS != venGraRet)
    {
        mbm_trace("mbm_png_decode:create bitmap error.");
		return;
    }

	// Step 5. Decode image
    decodeImgReq.bSync = TRUE;
    decodeImgReq.Handle = miipts_ven_handle;
    decodeImgReq.userData = (u32)pDecodeStatus;
    decodeImgReq.pfnCb = NULL;

    venCodecRet = ven_codec_decodeImage(hCodec,
                                        pBmp,
                                        pReadFileBuff,
                                        file_size,
                                        NULL, VEN_CODEC_NEXT_FRAME,
                                        &decParam,
                                        &decodeImgReq);

	ven_graphics_clearCanvas(g_MiiMainCanvas);
	ven_graphics_bitBlt(g_MiiMainCanvas, pBmp, 50, 50, 0);
	mbm_Display_DirectDrawImg(0, 0, 240, 320, 0);
            
    if (VEN_CODEC_SUCCESS != venCodecRet)
    {
        mbm_trace("mbm_png_decode:decode image error.");
		return;
    }
	
	pVenBitmap = (_ven_GraphicsBitmap*)pBmp;
	pVenDib = pVenBitmap->VenDIB;
	
//	pVenDib = ven_graphics_getBitmapDIB(&pBmp);
    data_ptr = pVenDib.pData;
	mbm_trace("mbm_png_decode:*pVenDib width = %d,height = %d,pitch = %d,bpp = %d.", pVenDib.width, pVenDib.height, pVenDib.pitch, pVenDib.bpp);
	imgSize = pVenDib.width*pVenDib.height*pVenDib.bpp/8;
	
	png_data_ptr = ven_os_mem_malloc(imgSize);
	mbm_trace("mbm_png_decode:png_data_ptr = %x.", png_data_ptr);
	ven_std_memcpy(png_data_ptr, data_ptr, imgSize);
	mbm_trace("mbm_png_decode:ven_std_memcpy");
	*img_p = png_data_ptr;

	ven_os_mem_free(pReadFileBuff);
	ven_file_wfclose(s_pngfile_handle);

	mbm_trace("mbm_png_decode:ven_os_mem_free pReadFileBuff.");
	
	closeImageCodecReq.bSync = TRUE;
    closeImageCodecReq.Handle = miipts_ven_handle;
    closeImageCodecReq.userData = 0;
    closeImageCodecReq.pfnCb = NULL;


    // Step 7. Release bitmap
    if (NULL != pBmp)
        ven_graphics_releaseBitmap(pBmp);

	mbm_trace("mbm_png_decode:ven_graphics_releaseBitmap.");
	
    // Step 8. Close image codec handle
    if (NULL != hCodec)
        venCodecRet = ven_codec_closeImgCodec(hCodec, &closeImageCodecReq);

 	mbm_trace("mbm_png_decode:ven_codec_closeImgCodec.");
	
    if (NULL != pDecodeStatus)
            ven_os_mem_free(pDecodeStatus);

	mbm_trace("mbm_png_decode:ven_os_mem_free pDecodeStatus.");
}

#endif /* MBM_IMIWORLD_SUPPORT */
